package com.mysoft.gfyx.common.time.impl;

import com.mysoft.gfyx.common.time.TimeService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.xml.bind.DatatypeConverter;

@Service
public class TimeServiceImpl implements TimeService {
    public String convertYearToTimeString(int year) {
        return Integer.toString(year) + "-01-01 00:00:00";
    }

    public String convertYearToNextYearTimeString(int year) {
        return (year + 1) + "-01-01 00:00:00";
    }

    public String convertTimeFormat(String time, String fromFormat, String toFormat) {
        SimpleDateFormat fromFormatter = new SimpleDateFormat(fromFormat);
        SimpleDateFormat toFormatter = new SimpleDateFormat(toFormat);

        try {
            Date date = fromFormatter.parse(time);
            return toFormatter.format(date);
        } catch (ParseException e) {
            return "";
        }

    }

    public Date getTodayZeroClock() {
        Date now = new Date(); //获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date todayZero = new Date();
        try {
            todayZero = sdf.parse(sdf.format(now));

        } catch (ParseException e) {
            System.out.println("竟然无法得到今天凌晨……");
        }
        return todayZero;
    }

    public String getLatestPeriod(Object dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date now = new Date();
            Date date = sdf.parse(dateStr.toString());
            if ((now.getTime() - date.getTime()) / 1000 > 60 * 60 * 24 * 365) {
                return "一年之前";
            } else if ((now.getTime() - date.getTime()) / 1000 > 60 * 60 * 24 * 30) {
                return "一月之前";
            } else if ((now.getTime() - date.getTime()) / 1000 > 60 * 60 * 24 * 7) {
                return "一周之前";
            } else {
                return "一周之内";
            }
        } catch (ParseException e) {
            System.out.println("竟然算不出时间……");
        }
        return "不明时间";
    }

    //获取当月第一天
    public Date getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 0);
        return calendar.getTime();
    }

    public String getPastDate(int past, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(today);
        return result;
    }

    public Date getDateAfter(int after, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + after);
        return calendar.getTime();
    }

    public String getYearsAgo(int year, Date date, String formatter) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - year);
        Date d = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        String result = sdf.format(d);
        return result;
    }

    public String getMonthAgo(int month, Date date, String formatter) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - month);
        Date d = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        String result = sdf.format(d);
        return result;
    }

    public boolean isRecent(String dateStr, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - month);
        Date d = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date t = sdf.parse(dateStr);
            return t.getTime() > d.getTime();
        } catch (ParseException e) {
            System.out.println("竟然算不出时间……");
            return false;
        }
    }

    public Date parseDateStr(String time) {
        //Calendar c = DatatypeConverter.parseDateTime(time);
        //return c.getTime();
        List<String> patterns = new ArrayList<>();
        patterns.add("yyyy-MM-dd'T'HH:mm:ssXXX");
        patterns.add("yyyy-MM-dd HH:mm:ss");

        for (int i = 0; i < patterns.size(); i++) {
            try {
                SimpleDateFormat formater = new SimpleDateFormat(patterns.get(i));
                Date d = formater.parse(time);
                return d;
            } catch (ParseException e) {
                continue;
            }
        }

        throw new RuntimeException("时间格式不正确");
    }
}
